Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./year2.RDS")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 414)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 11.43610 11.48563 11.53435 11.58228 11.62943 11.67585 11.72154 11.76653
## [9] 11.81085 11.85453 11.89757 11.94002 11.98190 12.02322 12.06401 12.10426
## [17] 12.14391 12.18294 12.22132 12.25903 12.29602 12.33228 12.36778 12.40249
## [25] 12.43638 12.46943 12.50160 12.53287 12.56321 12.59304 12.62275 12.65222
## [33] 12.68131 12.70992 12.73792 12.76520 12.79164 12.81711 12.84150 12.86469
## [41] 12.88657 12.90700 12.92587 12.94446 12.96385 12.98367 13.00352 13.02304
## [49] 13.04183 13.05952 13.07571 13.09004 13.10211 13.11154 13.11795 13.12161
## [57] 13.12313 13.12264 13.12028 13.11620 13.11053 13.10340 13.09497 13.08535
## [65] 13.07471 13.06317 13.05086 13.03794 13.02454 13.01079 12.99252 12.96694
## [73] 12.93627 12.90276 12.86863 12.83612 12.80746 12.78487 12.76399 12.73937
## [81] 12.71163 12.68141 12.64933 12.61603 12.58212 12.54826 12.51505 12.48314
## [89] 12.45314 12.42570 12.40144 12.38098 12.36232 12.34317 12.32376 12.30432
## [97] 12.28509 12.26631 12.24820 12.23099 12.21493 12.20023 12.18715 12.17590
## [105] 12.16665 12.15929 12.15364 12.14948 12.14664 12.14492 12.14412 12.14406
## [113] 12.14455 12.14538 12.14638 12.14734 12.14807 12.14839 12.15042 12.15580
## [121] 12.16364 12.17306 12.18315 12.19301 12.20176 12.20849 12.21231 12.21421
## [129] 12.21580 12.21717 12.21838 12.21951 12.22064 12.22183 12.22316 12.22471
## [137] 12.22655 12.22875 12.23139 12.23453 12.23826 12.24154 12.24349 12.24443
## [145] 12.24468 12.24457 12.24440 12.24450 12.24518 12.24677 12.24959 12.25395
## [153] 12.26017 12.26857 12.27948 12.29322 12.30963 12.32832 12.34887 12.37086
## [161] 12.39389 12.41753 12.44139 12.46505 12.48810 12.51012 12.53070 12.54944
## [169] 12.56592 12.58344 12.60518 12.63057 12.65905 12.69003 12.72294 12.75720
## [177] 12.79225 12.82751 12.86240 12.89635 12.92879 12.95914 12.98683 13.01129
## [185] 13.03194 13.04820 13.05951 13.06528 13.06768 13.06917 13.06965 13.06902
## [193] 13.06717 13.06402 13.05945 13.05336 13.04567 13.03625 13.02503 13.01188
## [201] 12.99672 12.97944 12.95590 12.92345 12.88422 12.84033 12.79391 12.74709
## [209] 12.70201 12.66080 12.62557 12.59091 12.55058 12.50552 12.45664 12.40488
## [217] 12.35114 12.29636 12.24146 12.18736 12.13499 12.08526 12.03911 11.99746
## [225] 11.96123 11.92708 11.89136 11.85449 11.81689 11.77900 11.74124 11.70403
## [233] 11.66781 11.63300 11.60003 11.56932 11.54129 11.51639 11.49502 11.47577
## [241] 11.45702 11.43892 11.42164 11.40534 11.39017 11.37629 11.36387 11.35306
## [249] 11.34402 11.33691 11.33188 11.32911 11.32874 11.33255 11.34165 11.35518
## [257] 11.37230 11.39216 11.41391 11.43670 11.45970 11.48204 11.50288 11.52138
## [265] 11.53668 11.55231 11.57194 11.59493 11.62066 11.64848 11.67777 11.70789
## [273] 11.73820 11.76809 11.79690 11.82401 11.84879 11.87060 11.88881 11.90495
## [281] 11.92098 11.93692 11.95277 11.96855 11.98427 11.99994 12.01557 12.03116
## [289] 12.04602 12.05956 12.07197 12.08345 12.09419 12.10441 12.11428 12.12400
## [297] 12.13379 12.14382 12.15431 12.16543 12.17740 12.19041 12.20374 12.21657
## [305] 12.22898 12.24106 12.25288 12.26452 12.27606 12.28757 12.29915 12.31085
## [313] 12.32277 12.33498 12.34755 12.36057 12.37509 12.39182 12.41040 12.43044
## [321] 12.45157 12.47339 12.49553 12.51762 12.53926 12.56008 12.57969 12.59772
## [329] 12.61379 12.62751 12.63949 12.65061 12.66098 12.67066 12.67973 12.68828
## [337] 12.69637 12.70410 12.71154 12.71876 12.72586 12.73290 12.73927 12.74442
## [345] 12.74851 12.75168 12.75411 12.75595 12.75736 12.75850 12.75953 12.76060
## [353] 12.76188 12.76352 12.76569 12.76854 12.77234 12.77705 12.78233 12.78787
## [361] 12.79332 12.79837 12.80268 12.80592 12.80778 12.80892 12.81021 12.81154
## [369] 12.81285 12.81403 12.81502 12.81572 12.81605 12.81592 12.81525 12.81396
## [377] 12.81195 12.80915 12.80547 12.80120 12.79667 12.79183 12.78665 12.78109
## [385] 12.77512 12.76869 12.76177 12.75432 12.74630 12.73768 12.72841 12.71859
## [393] 12.70832 12.69760 12.68642 12.67479 12.66268 12.65011 12.63705 12.62351
## [401] 12.60948 12.59496 12.57994 12.56441 12.54837 12.53179 12.51467 12.49701
## [409] 12.47883 12.46014 12.44095 12.42128 12.40114 12.38053
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 414)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 10.79445 10.87784 10.95975 11.04015 11.11901 11.19632 11.27204 11.34617
## [9] 11.41868 11.48955 11.55875 11.62626 11.69207 11.75615 11.81847 11.87906
## [17] 11.93795 11.99517 12.05074 12.10470 12.15707 12.20788 12.25714 12.30489
## [25] 12.35116 12.39597 12.43934 12.48131 12.52190 12.56074 12.59752 12.63233
## [33] 12.66527 12.69645 12.72596 12.75390 12.78038 12.80548 12.82932 12.85199
## [41] 12.87358 12.89420 12.91395 12.93133 12.94511 12.95576 12.96377 12.96962
## [49] 12.97378 12.97676 12.97901 12.98104 12.98331 12.98632 12.99054 12.99480
## [57] 12.99769 12.99930 12.99974 12.99909 12.99745 12.99494 12.99163 12.98764
## [65] 12.98305 12.97798 12.97251 12.96674 12.96077 12.95471 12.94799 12.94014
## [73] 12.93134 12.92177 12.91162 12.90107 12.89030 12.87951 12.86858 12.85727
## [81] 12.84555 12.83338 12.82073 12.80757 12.79386 12.77957 12.76466 12.74911
## [89] 12.73288 12.71594 12.69824 12.67977 12.65925 12.63579 12.60991 12.58214
## [97] 12.55301 12.52305 12.49277 12.46270 12.43338 12.40532 12.37905 12.35510
## [105] 12.32968 12.29927 12.26485 12.22740 12.18788 12.14728 12.10656 12.06670
## [113] 12.02867 11.99344 11.96198 11.93528 11.91429 11.90000 11.89054 11.88322
## [121] 11.87778 11.87395 11.87146 11.87005 11.86945 11.86941 11.86964 11.87134
## [129] 11.87565 11.88233 11.89110 11.90170 11.91386 11.92731 11.94179 11.95702
## [137] 11.97276 11.98872 12.00464 12.02025 12.03530 12.05239 12.07391 12.09921
## [145] 12.12768 12.15867 12.19155 12.22568 12.26042 12.29516 12.32924 12.36204
## [153] 12.39293 12.42126 12.44640 12.47135 12.49918 12.52936 12.56138 12.59472
## [161] 12.62885 12.66326 12.69742 12.73082 12.76293 12.79324 12.82123 12.84636
## [169] 12.86814 12.88868 12.91034 12.93287 12.95607 12.97970 13.00355 13.02738
## [177] 13.05098 13.07413 13.09659 13.11814 13.13857 13.15764 13.17513 13.19083
## [185] 13.20450 13.21592 13.22487 13.23113 13.23645 13.24253 13.24898 13.25546
## [193] 13.26161 13.26706 13.27146 13.27444 13.27565 13.27472 13.27130 13.26502
## [201] 13.25552 13.24245 13.22414 13.20003 13.17134 13.13934 13.10528 13.07040
## [209] 13.03595 13.00319 12.97337 12.94270 12.90709 12.86727 12.82397 12.77794
## [217] 12.72992 12.68064 12.63085 12.58127 12.53266 12.48575 12.44128 12.39999
## [225] 12.36261 12.32549 12.28490 12.24148 12.19586 12.14870 12.10064 12.05230
## [233] 12.00435 11.95741 11.91213 11.86916 11.82912 11.79267 11.76045 11.72990
## [241] 11.69831 11.66608 11.63361 11.60127 11.56947 11.53861 11.50907 11.48125
## [249] 11.45554 11.43234 11.41205 11.39504 11.38173 11.37220 11.36600 11.36276
## [257] 11.36209 11.36361 11.36694 11.37169 11.37748 11.38392 11.39064 11.39725
## [265] 11.40337 11.41084 11.42149 11.43493 11.45077 11.46860 11.48804 11.50867
## [273] 11.53011 11.55197 11.57383 11.59531 11.61601 11.63554 11.65349 11.67219
## [281] 11.69378 11.71758 11.74290 11.76906 11.79539 11.82119 11.84580 11.86852
## [289] 11.89130 11.91631 11.94316 11.97149 12.00091 12.03106 12.06156 12.09204
## [297] 12.12212 12.15144 12.17961 12.20626 12.23102 12.25352 12.27464 12.29549
## [305] 12.31608 12.33638 12.35638 12.37607 12.39543 12.41446 12.43314 12.45146
## [313] 12.46940 12.48695 12.50409 12.52083 12.53737 12.55393 12.57046 12.58690
## [321] 12.60320 12.61931 12.63519 12.65078 12.66603 12.68089 12.69532 12.70925
## [329] 12.72265 12.73545 12.74716 12.75745 12.76654 12.77463 12.78196 12.78872
## [337] 12.79513 12.80142 12.80779 12.81446 12.82164 12.82955 12.83752 12.84483
## [345] 12.85157 12.85782 12.86368 12.86925 12.87462 12.87988 12.88512 12.89045
## [353] 12.89594 12.90170 12.90783 12.91440 12.92185 12.93031 12.93947 12.94900
## [361] 12.95859 12.96791 12.97665 12.98448 12.99109 12.99683 13.00230 13.00752
## [369] 13.01250 13.01727 13.02184 13.02624 13.03048 13.03459 13.03858 13.04247
## [377] 13.04628 13.05004 13.05376 13.05738 13.06083 13.06412 13.06724 13.07022
## [385] 13.07305 13.07575 13.07832 13.08076 13.08308 13.08529 13.08740 13.08939
## [393] 13.09123 13.09293 13.09450 13.09592 13.09720 13.09835 13.09935 13.10022
## [401] 13.10095 13.10154 13.10199 13.10231 13.10249 13.10255 13.10249 13.10232
## [409] 13.10202 13.10160 13.10105 13.10037 13.09955 13.09860
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 414)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 10.85988 10.91968 10.97847 11.03624 11.09299 11.14872 11.20342 11.25707
## [9] 11.30969 11.36126 11.41177 11.46122 11.50960 11.55692 11.60315 11.64835
## [17] 11.69254 11.73572 11.77789 11.81905 11.85919 11.89830 11.93638 11.97344
## [25] 12.00945 12.04443 12.07836 12.11125 12.14308 12.17368 12.20293 12.23087
## [33] 12.25757 12.28309 12.30749 12.33082 12.35315 12.37453 12.39502 12.41469
## [41] 12.43359 12.45178 12.46932 12.48633 12.50284 12.51877 12.53404 12.54860
## [49] 12.56235 12.57522 12.58715 12.59806 12.60787 12.61651 12.62390 12.63011
## [57] 12.63526 12.63937 12.64246 12.64457 12.64571 12.64591 12.64519 12.64358
## [65] 12.64109 12.63776 12.63360 12.62864 12.62291 12.61642 12.60737 12.59460
## [73] 12.57913 12.56199 12.54419 12.52675 12.51069 12.49703 12.48304 12.46567
## [81] 12.44546 12.42297 12.39874 12.37330 12.34720 12.32099 12.29521 12.27040
## [89] 12.24710 12.22587 12.20724 12.19175 12.17798 12.16418 12.15041 12.13674
## [97] 12.12322 12.10993 12.09692 12.08425 12.07200 12.06023 12.04900 12.03837
## [105] 12.02832 12.01879 12.00975 12.00117 11.99302 11.98528 11.97793 11.97092
## [113] 11.96425 11.95788 11.95178 11.94593 11.94030 11.93487 11.93076 11.92872
## [121] 11.92822 11.92868 11.92954 11.93026 11.93028 11.92903 11.92596 11.92121
## [129] 11.91544 11.90885 11.90169 11.89415 11.88648 11.87888 11.87158 11.86480
## [137] 11.85875 11.85367 11.84976 11.84725 11.84637 11.84513 11.84174 11.83669
## [145] 11.83047 11.82357 11.81648 11.80969 11.80369 11.79897 11.79602 11.79532
## [153] 11.79738 11.80267 11.81170 11.82445 11.84031 11.85889 11.87977 11.90254
## [161] 11.92678 11.95209 11.97806 12.00426 12.03031 12.05577 12.08025 12.10333
## [169] 12.12459 12.14758 12.17567 12.20823 12.24460 12.28414 12.32623 12.37020
## [177] 12.41543 12.46126 12.50706 12.55219 12.59601 12.63786 12.67712 12.71313
## [185] 12.74526 12.77287 12.79531 12.81194 12.82614 12.84142 12.85735 12.87348
## [193] 12.88936 12.90458 12.91867 12.93121 12.94176 12.94986 12.95510 12.95701
## [201] 12.95517 12.94914 12.93600 12.91441 12.88634 12.85374 12.81856 12.78276
## [209] 12.74828 12.71709 12.69114 12.66473 12.63158 12.59275 12.54933 12.50238
## [217] 12.45298 12.40220 12.35111 12.30079 12.25230 12.20673 12.16513 12.12860
## [225] 12.09819 12.07009 12.04003 12.00848 11.97589 11.94269 11.90934 11.87629
## [233] 11.84399 11.81288 11.78342 11.75605 11.73123 11.70939 11.69100 11.67620
## [241] 11.66460 11.65579 11.64941 11.64505 11.64232 11.64084 11.64023 11.64008
## [249] 11.64001 11.63964 11.63856 11.63641 11.63277 11.62896 11.62646 11.62513
## [257] 11.62486 11.62553 11.62702 11.62921 11.63198 11.63521 11.63877 11.64255
## [265] 11.64643 11.65064 11.65546 11.66088 11.66685 11.67334 11.68033 11.68777
## [273] 11.69565 11.70392 11.71256 11.72153 11.73080 11.74035 11.75013 11.76059
## [281] 11.77203 11.78421 11.79691 11.80988 11.82289 11.83570 11.84808 11.85979
## [289] 11.87200 11.88586 11.90114 11.91759 11.93495 11.95299 11.97146 11.99012
## [297] 12.00871 12.02699 12.04472 12.06165 12.07754 12.09213 12.10678 12.12279
## [305] 12.13991 12.15785 12.17636 12.19516 12.21397 12.23254 12.25059 12.26784
## [313] 12.28403 12.29889 12.31215 12.32354 12.33318 12.34149 12.34861 12.35472
## [321] 12.35995 12.36446 12.36841 12.37196 12.37525 12.37845 12.38171 12.38518
## [329] 12.38901 12.39337 12.39830 12.40367 12.40933 12.41518 12.42107 12.42689
## [337] 12.43250 12.43777 12.44258 12.44680 12.45030 12.45295 12.45442 12.45458
## [345] 12.45362 12.45172 12.44906 12.44583 12.44222 12.43840 12.43456 12.43088
## [353] 12.42755 12.42475 12.42266 12.42148 12.41950 12.41532 12.40957 12.40288
## [361] 12.39588 12.38919 12.38345 12.37927 12.37729 12.37662 12.37597 12.37538
## [369] 12.37489 12.37452 12.37432 12.37431 12.37454 12.37504 12.37584 12.37698
## [377] 12.37849 12.38040 12.38276 12.38546 12.38838 12.39154 12.39494 12.39862
## [385] 12.40256 12.40681 12.41136 12.41624 12.42145 12.42702 12.43295 12.43923
## [393] 12.44582 12.45272 12.45991 12.46741 12.47520 12.48329 12.49167 12.50034
## [401] 12.50930 12.51854 12.52806 12.53785 12.54792 12.55827 12.56890 12.57982
## [409] 12.59102 12.60250 12.61428 12.62634 12.63870 12.65134
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")
keeping in case
#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")